package com.baosight.sczx.common.zm.cInterface.cs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.dao.Dao;

/**
 * 矿井 对外接口
 * <p>Title: Mine.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-1 上午07:01:49  严洪浩  Create
 *</pre>
 */
public class Mine {

	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	
	/**
	 * 矿井建模 中用到的 构造树
	 * <p>Function: getMineTree</p>
	 * <p>Description: </p>
	 * <p>return value:List<Map></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-1上午07:08:08 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static List<Map> getMineTree(Map<String, String> _argMap, boolean isShowAll){
		_argMap = _argMap == null ? new HashMap<String, String>():_argMap;
		
		if(isShowAll) {
			_argMap.put("fdStatus", "停产");
		}
		
		List<Map>  _list = new ArrayList();
		try {
			_list = dao.query("ZMCSMine.queryTree", _argMap);
		} catch (Exception e) {
			
		}
		return _list;
	}
	
	/**
	 * 判断一个矿井下是否有下级节点
	 * 包括生产单元、工程设计
	 * <p>Function: hasChildrenModule</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-3上午08:08:21 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static boolean hasChildren(Map<String,List<String>> _argMap, EiInfo eiInfo) {
		_argMap = _argMap == null ? new HashMap<String, List<String>>():_argMap;
		
		try {
			List<Map> _listPs = dao.query("ZMCSProductionSystem.validateMineCanDelete", _argMap);
			List<Map> _listEs = dao.query("ZMCSEngineeringDesign.validateMineCanDelete", _argMap);
			if(_listPs.size() < 1 && _listEs.size() < 1) {
				return false;
			}
			else if(_listPs.size() > 0){
				eiInfo.setStatus(-1);
				eiInfo.setMsg("矿井下有生产系统，不能删除！");
			}
			else if(_listEs.size() > 0){
				eiInfo.setStatus(-1);
				eiInfo.setMsg("矿井下有工程设计，不能删除！");
			}
			else {
				eiInfo.setStatus(-1);
				eiInfo.setMsg("删除失败！");
			}
		} catch (Exception e) {
			System.out.println("验证矿井下是否存在生产系统或工程设计出错：" + e.getMessage());
		}
		return true;
	}
	
	/**
	 * 
	 * <p>Function: getRootForDesignTree</p>
	 * <p>Description: 获取树根节点，从矿井信息表中</p>
	 * <p>return value:List<Map></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-10下午03:53:01 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static List<Map> getRootForDesignTree(){                                                              
		List<Map>  _list = new ArrayList();
		try {
			_list = dao.query("ZMCSMine.queryDesignTree", null);
		} catch (Exception e) {

		}
		return _list;
	}
	
	/**
	 * 构造矿井下拉列表Block
	 * <p>Function: getMineBlock</p>
	 * <p>Description: </p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-10上午08:33:35 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static EiBlock getMineBlock(String mineID, String blockID)
	{
		if(blockID == null || blockID.equals("")) {
			blockID = "cmes.cs.dataMineId";
		}
		EiBlock block = new EiBlock(blockID);
		
		EiColumn eiColumn = new EiColumn("value");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("display");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		Map<String, String> _map = new HashMap<String, String>();
		if(mineID != null && !mineID.trim().equals("")) {
			_map.put("fdKeyId", mineID);
		}
		
		List<Map> list = dao.query("ZMCSMine.queryInterface", _map);;
		
		block.setRows(list);
		return block;
	}
	
	/**
	 * 构造矿井下拉列表Block
	 * <p>Function: getMineBlock</p>
	 * <p>Description: </p>
	 * <p>_blockID: block名称</p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-8-31 12:56:17  王伟
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static EiBlock getMineBlock(String _blockID) {
		EiBlock block = new EiBlock(_blockID);
		EiColumn EiColumn = new EiColumn("display");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		EiColumn = new EiColumn("value");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("value", "");
		_map.put("display", "");
		_list.add(_map);

		List<Map> _listData = new ArrayList<Map>();
		try {
			_listData = dao.query("ZMCNMccyMain.queryTreeChild", null);
		} catch (Exception e) {}
		
		for(int i = 0; i < _listData.size(); i++) {
			_list.add(_listData.get(i)); 
		} 
		block.setRows(_list);
		return block;
	}
}
